package com.rc.saas.mini.service.enterprise;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.mini.common.base.TResponse;
import com.rc.saas.mini.mapper.enterprise.EnterpriseSubmemberApplyMapper;
import com.rc.saas.mini.model.enterprise.EnterpriseSubmember;
import com.rc.saas.mini.model.enterprise.EnterpriseSubmemberApply;
import com.rc.saas.mini.model.enterprise.EnterpriseSubmemberApplyExample;
import com.rc.saas.mini.model.member.MemberAccount;
import com.rc.saas.mini.model.member.MemberAccountExample;
import com.rc.saas.mini.model.member.MemberInfo;
import com.rc.saas.mini.service.elec.EnterpriseSubmemberService;
import com.rc.saas.mini.service.member.MemberAccountService;
import com.rc.saas.mini.service.member.MemberInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author wangzhengwei
 * @create 2021/4/25
 */
@Service
public class EnterpriseSubmemberApplyServiceImpl implements EnterpriseSubmemberApplyService {
    private static Logger logger = LoggerFactory.getLogger(EnterpriseSubmemberApplyServiceImpl.class);

    @Autowired
    private EnterpriseSubmemberApplyMapper enterpriseSubmemberApplyMapper;

    @Autowired
    private MemberInfoService memberInfoService;

    @Autowired
    private MemberAccountService memberAccountService;

    @Autowired
    private EnterpriseSubmemberService enterpriseSubmemberService;

    @Override
    public EnterpriseSubmemberApplyMapper getMapper() {
        return enterpriseSubmemberApplyMapper;
    }

    @Override
    public EnterpriseSubmemberApply selectBySubMemberId(Long subMemberId, String tenantCode) {
        EnterpriseSubmemberApplyExample enterpriseSubMemberApplyExample = new EnterpriseSubmemberApplyExample();
        enterpriseSubMemberApplyExample.createCriteria().andTenantCodeEqualTo(tenantCode).andSubMemberIdEqualTo(subMemberId);
        enterpriseSubMemberApplyExample.setOrderByClause("created_time desc");
        return enterpriseSubmemberApplyMapper.selectByExample(enterpriseSubMemberApplyExample).stream().findFirst().orElseGet(() -> null);
    }

    @Override
    public PageInfo<EnterpriseSubmemberApply> listPageByEntMember(String tenantCode, Long entMemberId, String keyWord, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<EnterpriseSubmemberApply> enterpriseSubmemberApplys = enterpriseSubmemberApplyMapper.listSubmemberApplyByEntMember(tenantCode, entMemberId, keyWord);
        return new PageInfo<>(enterpriseSubmemberApplys);
    }

    @Override
    public TResponse updateSubApply(String tenantCode, Long memberId, Long aprId, String status) {
        EnterpriseSubmemberApplyExample example = new EnterpriseSubmemberApplyExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode).andSubmemberApplyIdEqualTo(aprId);
        EnterpriseSubmemberApply submemberApply = enterpriseSubmemberApplyMapper.selectByExample(example).stream().findFirst().orElseGet(() -> null);
        if (submemberApply != null) {

            if ("1".equals(status)) {
                //查找子账号
                MemberInfo subMemberInfo = memberInfoService.selectByPrimaryKey(submemberApply.getSubMemberId());
                //查找主会员
                MemberInfo entMemberInfo = memberInfoService.selectByPrimaryKey(memberId);
                if (entMemberInfo == null) {
                    return TResponse.FAIL("主会员不存在！");
                }

                if (subMemberInfo != null) {
                    subMemberInfo.setBelongEnterpriseId(entMemberInfo.getBelongEnterpriseId());
                    subMemberInfo.setEnterpriseMemberId(entMemberInfo.getMemberId());
                    //会员类型(0个人 1企业主帐户 2企业子帐户 3互联互通用户) 默认0
                    subMemberInfo.setMemberType("2");
                    memberInfoService.updateByPrimaryKeySelective(subMemberInfo);

                    MemberAccount subMemberAccount = memberAccountService.selectByMemberId(tenantCode, subMemberInfo.getMemberId());
                    if (subMemberAccount != null) {
                        if (subMemberAccount.getEnterpriseAccount() == null) {
                            subMemberAccount.setEnterpriseAccount(BigDecimal.ZERO);
                        }
                        memberAccountService.updateByPrimaryKeySelective(subMemberAccount);
                    } else {
                        return TResponse.FAIL("子会员账户不存在！");
                    }
                    //插入sub_member
                    EnterpriseSubmember subMember = enterpriseSubmemberService.selectByPrimaryKey(subMemberInfo.getMemberId());
                    if (subMember == null) {
                        EnterpriseSubmember submemberVo = new EnterpriseSubmember();
                        submemberVo.setMemberId(subMemberInfo.getMemberId());
                        submemberVo.setTenantCode(tenantCode);
                        submemberVo.setSubmemberName(subMemberInfo.getNickname());
                        submemberVo.setBelongEnterpriseId(entMemberInfo.getBelongEnterpriseId());
                        submemberVo.setStatus("0");
                        submemberVo.setCreatedTime(new Date());
                        submemberVo.setLastUpdatedTime(new Date());
                        enterpriseSubmemberService.insertSelective(submemberVo);
                    } else {
                        subMember.setTenantCode(tenantCode);
                        subMember.setBelongEnterpriseId(entMemberInfo.getBelongEnterpriseId());
                        subMember.setSubmemberName(subMemberInfo.getNickname());
                        subMember.setStatus("0");
                        subMember.setLastUpdatedTime(new Date());
                        enterpriseSubmemberService.updateByPrimaryKeySelective(subMember);
                    }

                    submemberApply.setStatus(status);
                    submemberApply.setLastUpdatedBy(String.valueOf(memberId));
                    submemberApply.setLastUpdatedTime(new Date());
                    enterpriseSubmemberApplyMapper.updateByPrimaryKeySelective(submemberApply);

                } else {
                    return TResponse.FAIL("不存在子会员");
                }
            } else {
                submemberApply.setStatus(status);
                submemberApply.setLastUpdatedBy(String.valueOf(memberId));
                submemberApply.setLastUpdatedTime(new Date());
                enterpriseSubmemberApplyMapper.updateByPrimaryKeySelective(submemberApply);
            }
            return TResponse.SUCCESS();
        } else {
            logger.info("SubmemberApplyServiceImpl.reviewSubApply:submemberApply==null");
            return TResponse.FAIL("申请不存在");
        }
    }

    @Override
    public void insertByReturnId(EnterpriseSubmemberApply enterpriseSubmemberApply) {
        enterpriseSubmemberApplyMapper.insertByReturnId(enterpriseSubmemberApply);
    }

}
